package patchutil

import (
	"github.com/evanphx/json-patch"
	jsonpatchv2 "gomodules.xyz/jsonpatch/v2"
	"k8s.io/apimachinery/pkg/util/json"
)

func Diff(old, new interface{}) (jsonpatch.Patch, error) {
	oldBytes, err := json.Marshal(old)
	if err != nil {
		return nil, err
	}
	newBytes, err := json.Marshal(new)
	if err != nil {
		return nil, err
	}

	patches, err := jsonpatchv2.CreatePatch(oldBytes, newBytes)
	if err != nil {
		return nil, err
	}

	if patches == nil || len(patches) == 0 {
		return nil, nil
	}

	pBytes, err := json.Marshal(patches)
	if err != nil {
		return nil, err
	}

	decodePatch, err := jsonpatch.DecodePatch(pBytes)
	if err != nil {
		return nil, err
	}
	return decodePatch, nil
}

func GetDiffPaths(patch jsonpatch.Patch) []string {
	diffpaths := []string{}
	errors := []error{}

	for _, diff := range patch {
		path, err := diff.Path()
		if err != nil {
			continue
		}
		diffpaths = append(diffpaths, path)
	}
	if len(errors) > 0 {
		return nil
	}
	return diffpaths
}
